package smartcounters.androidclient.WebServices;

import java.util.LinkedList;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.os.Message;
import smartcounters.androidclient.model.AuthenticatedUserCredentials;
import smartcounters.androidclient.model.Bill;
import smartcounters.androidclient.model.eOperationsEnum;

public class BillingWebServiceClient implements OnResultReceiver {
	private LinkedList<OnResultReceiver> m_ResultReceivers;
	private String m_URL;
	
	public BillingWebServiceClient(String i_ServerIP) {
		m_ResultReceivers = new LinkedList<OnResultReceiver>();
		
		m_URL = "http://" + i_ServerIP + ":50171/WebServices/BillingWebService.asmx?WSDL";
	}
	
	public void GetMonthlyUsageSoFar(AuthenticatedUserCredentials i_AuthenticatedUserCredentials, int i_AccountID){
		String NAMESPACE = "http://smartcounters.org";
		String GET_SOAP_ACTION = "http://smartcounters.org/GetMonthlyUsageSoFar";
		String GET_METHOD_NAME = "GetMonthlyUsageSoFar";
		
		SoapObject request = new SoapObject(NAMESPACE, GET_METHOD_NAME);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		
		SoapObject usernameObject = new SoapObject(NAMESPACE, "i_AuthenticatedUserID");
		usernameObject.addProperty("i_AuthenticatedUserID", i_AuthenticatedUserCredentials.getID());
		SoapObject passwordObject = new SoapObject(NAMESPACE, "i_AuthenticatedUserPassword");
		passwordObject.addProperty("i_AuthenticatedUserPassword", i_AuthenticatedUserCredentials.getPassword());
		SoapObject accountIDObject = new SoapObject(NAMESPACE, "i_AccountID");
		accountIDObject.addProperty("i_AccountID", i_AccountID);
		
		request.addSoapObject(usernameObject);
		usernameObject.addSoapObject(passwordObject);
		passwordObject.addSoapObject(accountIDObject);
		
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(m_URL);
		
		MessageHandler handler = new MessageHandler();
		handler.RegisterResultReceiver(this);
		WebMethodAsyncInvoker webMethodAsyncInvoker = new WebMethodAsyncInvoker(androidHttpTransport, GET_SOAP_ACTION, envelope, handler, eOperationsEnum.GetMonthlyUsageSoFar);
		
		Thread thread = new Thread(webMethodAsyncInvoker);
		thread.start();
	}
	
	public void GetCurrentPrice(AuthenticatedUserCredentials i_AuthenticatedUserCredentials){
		String NAMESPACE = "http://smartcounters.org";
		String GET_SOAP_ACTION = "http://smartcounters.org/GetCurrentPrice";
		String GET_METHOD_NAME = "GetCurrentPrice";
		
		SoapObject request = new SoapObject(NAMESPACE, GET_METHOD_NAME);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		
		SoapObject usernameObject = new SoapObject(NAMESPACE, "i_AuthenticatedUserID");
		usernameObject.addProperty("i_AuthenticatedUserID", i_AuthenticatedUserCredentials.getID());
		SoapObject passwordObject = new SoapObject(NAMESPACE, "i_AuthenticatedUserPassword");
		passwordObject.addProperty("i_AuthenticatedUserPassword", i_AuthenticatedUserCredentials.getPassword());
		
		request.addSoapObject(usernameObject);
		usernameObject.addSoapObject(passwordObject);
		
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(m_URL);
		
		MessageHandler handler = new MessageHandler();
		handler.RegisterResultReceiver(this);
		WebMethodAsyncInvoker webMethodAsyncInvoker = new WebMethodAsyncInvoker(androidHttpTransport, GET_SOAP_ACTION, envelope, handler, eOperationsEnum.GetCurrentPrice);
		
		Thread thread = new Thread(webMethodAsyncInvoker);
		thread.start();
	}
	
	public void GetBill(AuthenticatedUserCredentials i_AuthenticatedUserCredentials, int i_AccountID, int i_Year, int i_Month, boolean i_SendBillByEmail)
	{	String NAMESPACE = "http://smartcounters.org";
		String GET_SOAP_ACTION = "http://smartcounters.org/GetBill";
		String GET_METHOD_NAME = "GetBill";
		
		SoapObject request = new SoapObject(NAMESPACE, GET_METHOD_NAME);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		
		SoapObject usernameObject = new SoapObject(NAMESPACE, "i_AuthenticatedUserID");
		usernameObject.addProperty("i_AuthenticatedUserID", i_AuthenticatedUserCredentials.getID());
		SoapObject passwordObject = new SoapObject(NAMESPACE, "i_AuthenticatedUserPassword");
		passwordObject.addProperty("i_AuthenticatedUserPassword", i_AuthenticatedUserCredentials.getPassword());
		SoapObject accountIDObject = new SoapObject(NAMESPACE, "i_AccountID");
		accountIDObject.addProperty("i_AccountID", i_AccountID);
		SoapObject yearObject = new SoapObject(NAMESPACE, "i_Year");
		yearObject.addProperty("i_Year", i_Year);
		SoapObject monthObject = new SoapObject(NAMESPACE, "i_Month");
		monthObject.addProperty("i_Month", i_Month);
		SoapObject sendBillByEmailObject = new SoapObject(NAMESPACE, "i_SendBillByEmail");
		sendBillByEmailObject.addProperty("i_SendBillByEmail", i_SendBillByEmail);
		
		request.addSoapObject(usernameObject);
		usernameObject.addSoapObject(passwordObject);
		passwordObject.addSoapObject(accountIDObject);
		accountIDObject.addSoapObject(yearObject);
		yearObject.addSoapObject(monthObject);
		monthObject.addSoapObject(sendBillByEmailObject);
		
		
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(m_URL);
		
		MessageHandler handler = new MessageHandler();
		handler.RegisterResultReceiver(this);
		WebMethodAsyncInvoker webMethodAsyncInvoker = new WebMethodAsyncInvoker(androidHttpTransport, GET_SOAP_ACTION, envelope, handler, eOperationsEnum.GetBill);
		
		Thread thread = new Thread(webMethodAsyncInvoker);
		thread.start();
	}
	
	public void RegisterResultReceiver(OnResultReceiver i_ResultReceiver) {
		m_ResultReceivers.add(i_ResultReceiver);
	}
	
	protected void OnResultReceived(Message i_Msg){
		if (i_Msg.obj instanceof ConnectionError){
			return;
		}
		
		SoapObject resultsRequestSOAP = (SoapObject) i_Msg.obj;
		if (resultsRequestSOAP.getPropertyCount() == 0) {
			i_Msg.obj = null;
		}
		else{
			if (eOperationsEnum.values()[i_Msg.what] == eOperationsEnum.GetBill){
				SoapObject soapObject = (SoapObject)resultsRequestSOAP.getProperty(0);
				i_Msg.obj = Bill.CreateFromSoapObject(soapObject);
			}
			else if (eOperationsEnum.values()[i_Msg.what] == eOperationsEnum.GetMonthlyUsageSoFar){
				i_Msg.obj = Double.parseDouble(resultsRequestSOAP.getProperty(0).toString());
			}
			else if (eOperationsEnum.values()[i_Msg.what] == eOperationsEnum.GetCurrentPrice){
				SoapObject soapObject = (SoapObject)resultsRequestSOAP.getProperty(0);
				i_Msg.obj = soapObject.getProperty("PriceForLiter");
			}
		}
		
		for (OnResultReceiver resultReceiver : m_ResultReceivers) {
			resultReceiver.ReceiveResult(i_Msg);
		}
	}
	
	public void ReceiveResult(Message i_Msg) {
		OnResultReceived(i_Msg);
	}
}
